package com.springboot.controller;

import com.springboot.dto.AllPatientList;
import com.springboot.model.OrderForm;
import com.springboot.model.UserLoginForm;
import com.springboot.service.DoctorViewAllPatientListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * 本控制台用于医生查看患者列表
 * 患者不重复；患者列表中显示的都是已就诊的患者
 * 可以显示每个患者预约的次数
 */
@Controller
public class DoctorViewAllPatientListController {

    @Autowired
    DoctorViewAllPatientListService doctorViewAllPatientListService;
    /**
     * 医生查看患者列表
     * chooseFlag用于判断当前是否使用患者姓名搜索患者
     */
    @CrossOrigin
    @RequestMapping(value = "/getEachPatient")
    public @ResponseBody
    String doctorViewPatientList(@RequestParam("flag") String chooseFlag,
                                 @RequestParam("patientName") String patientName,
                                 @RequestParam("doctorID") String tel)
    {
        System.out.println("医生查看患者列表");
        try {
            //不按照姓名搜索
            if(chooseFlag.equals("false"))
            {
                UserLoginForm userLoginForm=doctorViewAllPatientListService.findDoctorByTel(tel);
                int doctorId=userLoginForm.getUserId();
                //通过医生ID查找该医生的预约信息
                ArrayList<OrderForm> orderForms=doctorViewAllPatientListService.findOrderByDoctorId(doctorId);
                if(orderForms.size()==0)
                {
                    System.out.println("该医生没有预约信息");
                    return "空";
                }
                else
                {
                    int orderNum=0;
                    //数组存储进度为已就诊的患者id
                    int []patientId=new int[orderForms.size()+1];
                    for(int i=0;i<orderForms.size();i++)
                    {
                        OrderForm orderForm=orderForms.get(i);
                        if(orderForm.getProgress().equals("已就诊"))
                        {
                            patientId[orderNum]=orderForm.getPatientId();
                            System.out.println("预约该医生的患者："+orderForm.getPatientId());
                            orderNum++;
                        }
                    }
                    int []patientCount=new int[orderNum+1];
                    for(int i=0;i<orderNum;i++)
                    {
                        for(int j=i+1;j<orderNum;j++)
                        {
                            if(patientId[i]==patientId[j])
                            {
                                patientCount[i]++;
                                patientId[j]=0;
                            }
                        }
                    }
                    ArrayList<AllPatientList> allPatientLists=new ArrayList<>();
                    if(orderNum>0)
                    {
                        for(int i=0;i<orderNum;i++)
                        {
                            if(patientId[i]!=0)
                            {
                                AllPatientList allPatientList=new AllPatientList();
                                UserLoginForm patient=doctorViewAllPatientListService.findPatientByUserId(patientId[i]);
                                System.out.println("预约该医生不重复的患者："+patient.getUserId());
                                allPatientList.setLogoLink(patient.getLogoLink());
                                allPatientList.setPatientName(patient.getUserName());
                                allPatientList.setOrderNumber(patientCount[i]+1);
                                allPatientList.setPatientTel(patient.getUserTel());
                                allPatientLists.add(allPatientList);
                            }
                        }
                        //按预约日期排序
                        Comparator<AllPatientList> comparator = new Comparator<AllPatientList>(){
                            public int compare(AllPatientList s1, AllPatientList s2) {
                                //按照预约次数排序
                                return s2.orderNumber.compareTo(s1.orderNumber);
                            }
                        };
                        //这里就会自动根据规则进行排序
                        Collections.sort(allPatientLists,comparator);
                        System.out.println(allPatientLists.toString());
                        return allPatientLists.toString();
                    }
                    else
                    {
                        return "空";
                    }
                }
            }
            //chooseFlag==true表示使用智能化搜索：在患者列表使用患者姓名查找
            else
            {
                UserLoginForm userLoginForm=doctorViewAllPatientListService.findDoctorByTel(tel);
                int doctorId=userLoginForm.getUserId();
                //通过医生ID查找该医生的预约信息
                ArrayList<OrderForm> orderForms=doctorViewAllPatientListService.findOrderByDoctorId(doctorId);
                if(orderForms.size()==0)
                {
                    System.out.println("该医生没有预约信息");
                    return "空";
                }
                else
                {
                    int orderNum=0;
                    //数组存储进度为已就诊且患者姓名为搜索姓名的患者id
                    int []patientId=new int[orderForms.size()];
                    for(int i=0;i<orderForms.size();i++)
                    {
                        OrderForm orderForm=orderForms.get(i);
                        int id=orderForm.getPatientId();
                        UserLoginForm patient=doctorViewAllPatientListService.findPatientByUserId(id);
                        String userName=patient.getUserName();
                        String orderTime = patient.getUserTel();
                        int tag=1;
                        if(orderForm.getProgress().equals("已就诊")&&userName.contains(patientName))
                        {
                            tag=0;
                            patientId[orderNum]=orderForm.getPatientId();
                            System.out.println("符合搜索要求的患者："+patientId[orderNum]);
                            orderNum++;
                        }
                        if(orderForm.getProgress().equals("已就诊")&&orderTime.contains(patientName)&&tag==1)
                        {
                            patientId[orderNum]=orderForm.getPatientId();
                            System.out.println("符合搜索要求的患者："+patientId[orderNum]);
                            orderNum++;
                        }
                    }
                    int []patientCount=new int[orderNum+1];
                    for(int i=0;i<orderNum;i++)
                    {
                        for(int j=i+1;j<orderNum;j++)
                        {
                            if(patientId[i]==patientId[j])
                            {
                                patientCount[i]++;
                                patientId[j]=0;
                            }
                        }
                    }
                    ArrayList<AllPatientList> allPatientLists=new ArrayList<>();
                    if(orderNum>0)
                    {
                        for(int i=0;i<orderNum;i++)
                        {
                            if(patientId[i]!=0)
                            {
                                AllPatientList allPatientList=new AllPatientList();
                                UserLoginForm patient=doctorViewAllPatientListService.findPatientByUserId(patientId[i]);
                                System.out.println("符合搜索要求的不重复的患者："+patientId[i]);
                                allPatientList.setLogoLink(patient.getLogoLink());
                                allPatientList.setOrderNumber(patientCount[i]+1);
                                allPatientList.setPatientName(patient.getUserName());
                                allPatientList.setPatientTel(patient.getUserTel());
                                allPatientLists.add(allPatientList);
                            }
                        }
                        //按预约日期排序
                        Comparator<AllPatientList> comparator = new Comparator<AllPatientList>(){
                            public int compare(AllPatientList s1, AllPatientList s2) {
                                //按照预约次数排序
                                return s2.orderNumber.compareTo(s1.orderNumber);
                            }
                        };
                        //这里就会自动根据规则进行排序
                        Collections.sort(allPatientLists,comparator);
                        System.out.println(allPatientLists.toString());
                        return allPatientLists.toString();
                    }
                    else
                    {
                        return "空";
                    }
                }
            }
        } catch (Exception e) {
            return "False";
        }
    }
}